package cn.livingCloud.service.impl.operation;

import cn.livingCloud.context.BeanProvider;
import cn.livingCloud.dto.TextReplyRuleFormDto;
import cn.livingCloud.entity.content.ReplyRule;
import cn.livingCloud.entity.content.TextReplyContent;
import cn.livingCloud.repository.TextReplyContentMapper;
import org.apache.commons.lang.StringUtils;

/**
 * Created by 唐旭 on 2015/12/26.
 */
public class TextReplyRuleFormDtoUpdater extends ReplyRuleFormDtoUpdater {
    protected transient TextReplyContentMapper textMapper = BeanProvider.getBean(TextReplyContentMapper.class);
    private TextReplyRuleFormDto formDto;

    public TextReplyRuleFormDtoUpdater(TextReplyRuleFormDto formDto) {
        super(formDto);
        this.formDto = formDto;
    }

    public void update() {
        ReplyRule replyRule = super.updateRule();
        String uuid = formDto.getUuid();
        if (StringUtils.isEmpty(uuid)) {
            createTextRule(replyRule);
        } else {
            updateTextRule(replyRule);
        }
    }

    private void updateTextRule(ReplyRule replyRule) {
        TextReplyContent textContent = textMapper.findTextContent(formDto.getContent().getUuid());
        formDto.update(textContent);
        textMapper.update(textContent);
    }

    private void createTextRule(ReplyRule replyRule) {
        TextReplyContent content = new TextReplyContent();
        content.setRule_id(replyRule.getUuid());
        formDto.update(content);
        textMapper.save(content);
    }
}
